clear all
set more off

* when using portable STATA
cd           "\CGR_credit\"
local out2 = "\CGR_credit\outregsQJE\"
use "step001_credit_QJE.dta", clear

tsset firm_id time

*** define treatment as given inflation TIMES treatment status
gen Treat=treatment_old*infl


* multiply by 100 mean utilization rate
gen    util_ratio_mean0 = util_ratio_mean * 100
drop   util_ratio_mean
rename util_ratio_mean0 util_ratio_mean

* rename variable and create first difference
rename util_ratio_mean ur_crl
gen dur_crl=d.ur_crl

*** winsorize utilizatior rate and delta(utilization rate)
winsor ur_crl, gen(ur_crl_w) p(0.02)
winsor dur_crl, gen(dur_crl_w) p(0.02)

*** winsorize firm expected price change
winsor dprez, gen(dprez_w) p(0.02)


*** transform variable sitimp5 into 3-category variable (not going to use it)
recode sitimp5 (1 2 = 1) (3 = 2) (4 5 = 3), gen(sitimp3)

local controls0=" l2.dprez_w  i.l2.sitimp5 i.l2.occtot i.l2.sitgen i.l2.promig i.l2.liquid  "	


***======================================================
*** 					IRF: cumulative [from levels]
***						No firm FE
***======================================================
keep if time>=208


***======================================================
*** 			responses by sector
***======================================================


*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f`hor'.ur_crl-l.ur_crl
		winsor FC, gen(FC_w2) p(0.02)	
	
		ivreghdfe FC_w2 `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if setton3==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'Table05_util_sector.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'Table05_util_sector.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}



***======================================================
*** 			responses by firm size
***======================================================

	
gen size=1 	if add_new<=99 
replace size=2 if add_new>99 & add_new<=299
replace size=3 if add_new>=300 & add_new~=.

	
*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f`hor'.ur_crl-l.ur_crl
		winsor FC, gen(FC_w2) p(0.02)	
		
		ivreghdfe FC_w2 `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if size==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'Table05_util_size.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'Table05_util_size.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}

	
***======================================================
*** 			responses by export share
***======================================================

gen exD=1 	if export==1
replace exD=2 if export==2
replace exD=3 if (export==3 | export==4) & export~=.


*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f`hor'.ur_crl-l.ur_crl
		winsor FC, gen(FC_w2) p(0.02)	
		
		ivreghdfe FC_w2 `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if exD==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'Table05_util_export.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'Table05_util_export.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}
*/
	
***======================================================
*** 			responses by geography
***======================================================

gen geo=1 	if (area==1 | area==2) & area~=.
replace geo=2 if area==3
replace geo=3 if  area==4


*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f`hor'.ur_crl-l.ur_crl
		winsor FC, gen(FC_w2) p(0.02)
		
		ivreghdfe FC_w2 `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if geo==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'Table05_util_geo.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'Table05_util_geo.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}				
		
